#!/usr/bin/env bash
set -Eeuo pipefail

####################
# Global variables
####################
APP="OHEMU"
SUPPORT="https://github.com/SSRVodka/oh-qemu-docker"
PROCESS="${APP,,}"
PROCESS="${PROCESS// /-}"

RUN_LOCAL=0

: "${OHOS_IMG_DIR_LIST:=""}"

: "${INSTANCE_NUM:=1}"
: "${INSTANCE_SN_LIST:="8823456789"}"
: "${INSTANCE_MAC_LIST}:="02:CB:4D:2B:B1:A5"}"

OHOS_IMG_DIR_LIST="${OHOS_IMG_DIR_LIST//,/ }"
INSTANCE_SN_LIST="${INSTANCE_SN_LIST//,/ }"
INSTANCE_MAC_LIST="${INSTANCE_MAC_LIST//,/ }"
read -r -a IMG_DIR_LIST <<< "${OHOS_IMG_DIR_LIST}"
read -r -a MAC_LIST <<< "${INSTANCE_MAC_LIST}"

: "${RAM_SIZE:="4096M"}"
: "${SMP:="6"}"
: "${STORAGE:="/storage"}"

cd /run

# Create Environment variable for /run
echo 'export PATH=$PATH:/run' >> ~/.bashrc

. init.sh
# network helpers
. network.sh

init ${RAM_SIZE} ${STORAGE} "Y" ${INSTANCE_NUM} # RAM check

info "Initializing network..."
initializeDNS
_getNetworkInfo ""  # Get IP, Gateway, Interface(DEV)

info "Host: $HOST  IP: $IP  Gateway: $GATEWAY  Interface: $VM_NET_DEV"
    [ -f /etc/resolv.conf ] && grep '^nameserver*' /etc/resolv.conf
echo

CNT=0
for sn in $INSTANCE_SN_LIST
do
    # No parameter: random MAC address
    _getNetworkInfo ${MAC_LIST[$CNT]}   # Get random MAC
    _configureNAT $CNT $VM_NET_MAC      # Get NET_OPTS
    KERNEL_PARAMS="\
    ip=dhcp \
    loglevel=7 \
    console=ttyS${CNT},115200 \
    init=init root=/dev/ram0 \
    rw \
    ohos.boot.hardware=virt \
    default_boot_device=10007000.virtio_mmio \
    sn=${sn} \
    ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required \
    ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required \
    "
    KERNEL_PARAMS=$(echo "$KERNEL_PARAMS" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -s ' ')
    OHOS_IMG_DIR=${IMG_DIR_LIST[$CNT]}
    ARGS="\
    -machine pc-i440fx-6.0 \
    -smp ${SMP} \
    -m $RAM_SIZE \
    -cpu host \
    -boot c \
    -enable-kvm \
    -nographic \
    -vga virtio \
    -display vnc=:${CNT} \
    -rtc base=utc,clock=host \
    -device es1370 \
    -initrd ${OHOS_IMG_DIR}/ramdisk.img \
    -kernel ${OHOS_IMG_DIR}/bzImage \
    ${NET_OPTS} \
    -drive file=${OHOS_IMG_DIR}/updater.img,if=virtio,media=disk,format=raw,index=0 \
    -drive file=${OHOS_IMG_DIR}/system.img,if=virtio,media=disk,format=raw,index=1 \
    -drive file=${OHOS_IMG_DIR}/vendor.img,if=virtio,media=disk,format=raw,index=2 \
    -drive file=${OHOS_IMG_DIR}/sys_prod.img,if=virtio,media=disk,format=raw,index=3 \
    -drive file=${OHOS_IMG_DIR}/chip_prod.img,if=virtio,media=disk,format=raw,index=4 \
    -drive file=${OHOS_IMG_DIR}/userdata.img,if=virtio,media=disk,format=raw,index=5 \
    -append \"${KERNEL_PARAMS}\" \
    "
    info "Instance $CNT arguments:"
    echo $ARGS
    ARGS=$(echo "$ARGS" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -s ' ')
    CNT=$(($CNT+1))
    echo "nohup qemu-system-x86_64 ${ARGS} 2>&1 > /logs/ohemu-$CNT.log &" >> main.sh
done

if [ $CNT -ne $INSTANCE_NUM ]; then
    warn "INSTANCE_NUM doesn't match the length of SN/MAC list"
fi

. main.sh

while true
do
    sleep 2
done
